﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Controller_App
{
    /// <summary>
    /// Form um Querys zu konfigurieren
    /// </summary>
    public partial class frm_Query : Form
    {
        private int _I_Count = -1;
        private int _O_Count = -1;
        private IVisual.I_Controller _ctrl;

        public frm_Query()
        {
            InitializeComponent();
        }

        public void INIT(int _I_Count, int _O_Count, IVisual.I_Controller _ctrl)
        {
            this._ctrl = _ctrl;
            this._I_Count = _I_Count;
            this._O_Count = _O_Count;

            RECALC_ROWS();
        }

        private void RECALC_ROWS()
        {
            this._dgv_in.Rows.Clear();
            for (int i = 1; i <= _I_Count; i++)
                if(this._cb_schwellenwert.Checked)
                    this._dgv_in.Rows.Add(false);
                else
                    this._dgv_in.Rows.Add("0");

            this._dgv_out.Rows.Clear();
            for (int i = 1; i <= _O_Count; i++)
                if (this._cb_schwellenwert.Checked)
                    this._dgv_out.Rows.Add(false);
                else
                    this._dgv_out.Rows.Add("0");
        }

        private void _dgv_in_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
        {
            if (!this._cb_schwellenwert.Checked)
            {
                double _tmp;
                e.ParsingApplied = double.TryParse((string)e.Value, out _tmp);
                e.Value = _tmp.ToString();
            }
        }

        private void _cmd_query_Click(object sender, EventArgs e)
        {
            float[] _INP = new float[this._I_Count];
            for (int i = 0; i < _INP.Length; i++)
            {
                if (this._cb_schwellenwert.Checked)
                    _INP[i] = (bool)this._dgv_in.Rows[i].Cells[0].Value == true ? 1 : 0;
                else
                    _INP[i] = float.Parse(this._dgv_in.Rows[i].Cells[0].Value.ToString());
            }

            float[] _OUT = this._ctrl.Compute(_INP);

            for(int i=0; i<_OUT.Length; i++)
            {
                if (this._cb_schwellenwert.Checked)
                    this._dgv_out.Rows[i].Cells[0].Value = (decimal)_OUT[i] >= this._nud_schwellenwert.Value ? 1 : 0;
                else
                    this._dgv_out.Rows[i].Cells[0].Value = _OUT[i].ToString();
            }
        }

        private void _cmd_close_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void _cb_schwellenwert_CheckedChanged(object sender, EventArgs e)
        {
            this._nud_schwellenwert.Enabled = this._cb_schwellenwert.Checked;

            this._dgv_in.Columns.Clear();
            this._dgv_out.Columns.Clear();
            if (this._cb_schwellenwert.Checked)
            {
                DataGridViewCheckBoxColumn CB_COL;
                CB_COL = new DataGridViewCheckBoxColumn();
                CB_COL.HeaderText = "Eingaben";
                CB_COL.Name = CB_COL.HeaderText;
                CB_COL.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
                this._dgv_in.Columns.Add(CB_COL);

                CB_COL = (DataGridViewCheckBoxColumn)CB_COL.Clone();
                CB_COL.HeaderText = "Ausgaben";
                CB_COL.Name = CB_COL.HeaderText;
                this._dgv_out.Columns.Add(CB_COL);
            }
            else
            {
                DataGridViewTextBoxColumn CB_COL;
                CB_COL = new DataGridViewTextBoxColumn();
                CB_COL.HeaderText = "Eingaben";
                CB_COL.Name = CB_COL.HeaderText;
                CB_COL.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
                this._dgv_in.Columns.Add(CB_COL);

                CB_COL = (DataGridViewTextBoxColumn)CB_COL.Clone();
                CB_COL.HeaderText = "Ausgaben";
                CB_COL.Name = CB_COL.HeaderText;
                this._dgv_out.Columns.Add(CB_COL);
            }

            RECALC_ROWS();
        }
    }
}
